package com.sgb.kjwl.viewmodel.packers;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.FindJobEntity;
import com.sgb.kjwl.model.entity.FindJobRequestEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.packers.MapFindWorkActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author liwei
*@date 2020/11/10
*description:
*/
public class MapFindWorkModel extends BaseViewModel {

    public ObservableField<FindJobRequestEntity> filterJson = new ObservableField<>(new FindJobRequestEntity());

    private MapFindWorkActivity activity;
    private HomeRepertory repertory;

    private int page = 1;
    private int limit = 10;

    public MapFindWorkModel(MapFindWorkActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /**
     * 列表
     * @param firstCategoryNo
     * @param secondCategoryNo
     * @param thirdCategoryNo
     * @param fourCategoryNo
     * @param fiveCategoryNo
     * @param sixCategoryNo
     * @param cityNo
     * @param b
     */
    public void getNewList( String firstCategoryNo, String secondCategoryNo, String thirdCategoryNo, String fourCategoryNo,String fiveCategoryNo,String sixCategoryNo, String cityNo, boolean b) {
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("areacode", repertory.getAreaCode());
        map.put("limit", limit);
        map.put("page", page);

        if (!"".equals(firstCategoryNo) && firstCategoryNo != null) {
            map.put("firstCategoryNo", firstCategoryNo);
        }
        if (!"".equals(secondCategoryNo) && secondCategoryNo != null) {
            map.put("secondCategoryNo", secondCategoryNo);
        }
        if (!"".equals(thirdCategoryNo) && thirdCategoryNo != null) {
            map.put("thirdCategoryNo", thirdCategoryNo);
        }
        if (!"".equals(cityNo) && cityNo != null) {
            map.put("cityNo", cityNo);
        }

        if (!"".equals(fourCategoryNo) && fourCategoryNo != null) {
            map.put("fourCategoryNo", fourCategoryNo);
        }

        if (!"".equals(fiveCategoryNo) && fiveCategoryNo != null) {
            map.put("fifthCategoryNo", fiveCategoryNo);
        }
        if(!"".equals(sixCategoryNo)&&sixCategoryNo!=null){
            map.put("sixthCategoryNo",sixCategoryNo);
        }

        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getFindJob(map).enqueue(new Callback<BaseEntity<List<FindJobEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindJobEntity>>> call, Response<BaseEntity<List<FindJobEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showList(response.body().getData(), b, response.body().getData() == null || response.body().getData().size() < limit);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindJobEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 详情
     * @param id
     */
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("constructdetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    showUrl(url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void showUrl(String url) {
        WebViewX5HostActivity.start(activity, url);
    }


    /**
     * 筛选
     * @param isLoadMore
     */
    public void getList(boolean isLoadMore) {
        if (isLoadMore) {
            page = page + 1;
        } else {
            page = 1;
        }
        filterJson.get().setAreacode(repertory.getAreaCode());
        filterJson.get().setPage(page);
        filterJson.get().setLimit(limit);

        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getFindJob(filterJson.get()).enqueue(new Callback<BaseEntity<List<FindJobEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindJobEntity>>> call, Response<BaseEntity<List<FindJobEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showList(response.body().getData(), isLoadMore, response.body().getData() == null || response.body().getData().size() < limit);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindJobEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 工程
     */
    public void getProTypeList() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobFirstCategory(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setProData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 专业
     * @param cid
     */
    public void getSecondCategory(String cid) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setMajorData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 业务
     * @param cid
     * @param isAll
     */
    public void getThrCategory(String cid, boolean isAll) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setBusinessData(response.body().getData(), isAll);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getFourCategory(String cid) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getFiveCategory(String cid) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getSixCategory(String cid) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 付款方式
     */
    public void getPf() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getfindJobSysPayWay(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public String getAreaCode() {
        return repertory.getAreaCode();
    }

    // ------------------------------------------所在地区 -----------------------------------------------

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getAreaSecondAndThird(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();

            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
